package com.hmall.item.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 * @Author: suren@517na.com
 * @CreateTime: 2025-04-25
 * @Description:
 */
public class Test2 {
    public static void main(String[] args) {
//        Scanner in = new Scanner(System.in);
//        13
//        3,3,7,4,4,4,4,7,7,3,5,5,5
//        53,80,68,24,39,76,66,16,100,55,53,80,55
        int count = 0;
        List<Integer> num = new ArrayList<>(Arrays.asList(3,3,7,4,4,4,4,7,7,3,5,5,5));
        List<Integer> score = new ArrayList<>(Arrays.asList(53,80,68,24,39,76,66,16,100,55,53,80,55));
//        while (in.hasNextInt()) {
//            num = score;
//            if (in.hasNextLine()) {
//                while (in.hasNextInt()) {
//                    num.a dd(in.nextInt());
//                    if (in.hasNextLine()) {
//                        while (in.hasNextInt()) {
//                            score.add(in.nextInt());
//                        }
//                    }
//                }
//            }
//            if (num.size() == count && score.size() == count) {
//                break;
//            }
//        }
        test(count, num, score);
    }


    public static void test(int count, List<Integer> nums, List<Integer> scores) {
        if (nums.size() != scores.size()) {
            return;
        }
        Map<Integer, List<Integer>> scoreMap = new HashMap<>();
        for (int i = 0; i < nums.size(); i++) {
            Integer key = nums.get(i);
            if (scoreMap.containsKey(key)) {
                scoreMap.get(key).add(scores.get(i));
            } else {
                List<Integer> list = new ArrayList<>();
                list.add(scores.get(i));
                scoreMap.put(key, list);
            }
        }
        Map<Integer, List<Integer>> scoreCountMap = new HashMap<>();
        List<Integer> totalScores = new ArrayList<>();
        for (Map.Entry<Integer, List<Integer>> entry : scoreMap.entrySet()) {
            //成绩小于三次排除该选手
            List<Integer> list = entry.getValue();
            if (list.size() < 3) {
                continue;
            }
            //个人成绩排名
            list.sort((o1, o2) -> o2 - o1);
            int sumScore = list.get(0) + list.get(1) + list.get(2);
            if (!totalScores.contains(sumScore)) {
                totalScores.add(sumScore);
            }
            //相同成绩的加到一组里
            if (scoreCountMap.containsKey(sumScore)) {
                scoreCountMap.get(sumScore).add(entry.getKey());
            } else {
                List<Integer> numList = new ArrayList<>();
                numList.add(entry.getKey());
                scoreCountMap.put(sumScore, numList);
            }
        }

        //相同分数下，运动员编号由大到小排序
        for (List<Integer> value : scoreCountMap.values()) {
            value.sort((o1, o2) -> o2 - o1);
        }

        //总分排序
        totalScores.sort((o1, o2) -> o2 - o1);

        //输出结果
        StringBuilder result = new StringBuilder();
        for (Integer totalScore : totalScores) {
            for (Integer integer : scoreCountMap.get(totalScore)) {
                result.append(integer).append(",");
            }
        }
        result.deleteCharAt(result.length()-1);
        System.out.println(result);
    }

}
